﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;

using System.Web.SessionState;

using XMK.Tadong.Model.Cond;
using XMK.Tadong.Biz;
using XMK.Framework.Model;
using XMK.Tadong.Model;

namespace XMK.Tadong.ServiceWeb.Handlers
{
    /// <summary>
    /// LoginHandler 的摘要说明
    /// </summary>
    public class LoginAuth : IHttpHandler, IRequiresSessionState
    {

        public void ProcessRequest(HttpContext context)
        {

            string str = context.Request.Form["updateSession"];

            if (str == "1")
            {
                context.Session["_userid"] = context.Session["_userid"];
                return;
            }
 

            
            context.Response.ContentType = "text/plain";
            ResponseInfo responseInfo = new ResponseInfo();
            string paraStr = context.Request["data"] ?? "";
            if (string.IsNullOrEmpty(paraStr))
            {
                responseInfo.State = 1;
                responseInfo.ErrMsg = "用户名和密码不能为空";
                context.Response.Write(Utils.JsonHelper.ToJson(responseInfo));
                context.Response.End();
            }

            Base_RM_UserInfoBiz userBiz = new Base_RM_UserInfoBiz();
            Base_LoginCond loginCond = Utils.JsonHelper.FromJson<Base_LoginCond>(paraStr);
            responseInfo = userBiz.GetUserInfoByNamePassword(loginCond);
            if (responseInfo.Data != null)
            {
                Base_UserInfo userinfo = (Base_UserInfo)responseInfo.Data;
                context.Session.Timeout = 120;
                context.Session["_userid"] = userinfo.OEMSession;
                context.Session["userid"] = userinfo.USER_ID;
                context.Session["username"] = userinfo.USER_NAME;
                context.Session["useraccount"] = userinfo.USER_ACCOUNT;
                context.Session["usercode"] = userinfo.USER_CODE;
                context.Session["lang"] = userinfo.USER_LANG == 1 ? "zh" : "en";

                //记录日志
                XMKLogInfo logModel = new XMKLogInfo();
                logModel.Keyword = "login";
                logModel.LogDesc = string.Format("【{0}】【{1}】login system",
                    userinfo.USER_NAME, userinfo.USER_ACCOUNT);
                logModel.OperateTime = DateTime.Now;
                logModel.OperateType = LogOperateType.Login;
                logModel.UserId = userinfo.USER_ID;
                logModel.IP = context.Request.ServerVariables["REMOTE_ADDR"].Equals("::1") ? "127.0.0.1" : context.Request.ServerVariables["REMOTE_ADDR"];
                XMKLogBiz.WriteLog(logModel);
            }
            else
            {
                responseInfo.State = 1;
                responseInfo.ErrMsg = "Account error!";
            }
            context.Response.Write(Utils.JsonHelper.ToJson(responseInfo));
            context.Response.End();
        }

        public bool IsReusable
        {
            get
            {
                return false;
            }
        }
    }
}